package com.atguigu.Flink.state;

import com.atguigu.Flink.POJO.WindSpeedSensor;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Comparator;

public class Flink06_KeyedListState {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //需求：检测每种传感器的风速，如果连续的两个风速差值为10，就输出报警
        SingleOutputStreamOperator<WindSpeedSensor> ds = env.socketTextStream("hadoop102", 8888)
                .map(
                        line -> {
                            String[] split = line.split(",");
                            return new WindSpeedSensor(split[0], Integer.valueOf(split[1]), Long.valueOf(split[2]));
                        }
                );
            //针对每种传感器输出最高的三个风速
                ds.keyBy(
                        k->k.getId()
                ).process(
                        new KeyedProcessFunction<String, WindSpeedSensor, String>() {
                            //声明列表状态，维护三个最高的水位值
                            private ListState<Integer> liststate;

                            @Override
                            public void open(Configuration parameters) throws Exception {
                                //初始化状态
                                ListStateDescriptor<Integer> integerListStateDescriptor = new ListStateDescriptor<Integer>("list",Types.INT);
                                liststate = getRuntimeContext().getListState(integerListStateDescriptor);
                            }

                            @Override
                            public void processElement(WindSpeedSensor windSpeedSensor, KeyedProcessFunction<String, WindSpeedSensor, String>.Context context, Collector<String> collector) throws Exception {
                                //获取状态
                                Iterable<Integer> iterable = liststate.get();
                                ArrayList<Integer> top3 = new ArrayList<>();
                                for (Integer integer : iterable) {
                                    top3.add(integer);
                                }
                                top3.add(windSpeedSensor.getSpeed());
                                if(top3.size() <=3){
                                    //直接更新到状态中
                                    liststate.update(top3);
                                }else{
                                    //给集合排序
                                    top3.sort(new Comparator<Integer>() {
                                        @Override
                                        public int compare(Integer o1, Integer o2) {
                                            return  o2.compareTo(o1);
                                        }
                                    });
                                    top3.remove(3) ;
                                    //更新到状态中
                                    liststate.update(top3);
                                }
                                collector.collect("最高的三个风速："+top3);
                            }
                        }
                ).print();



        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
